package com.supplychainapi.utils;

import com.supplychainapi.model.ScmTbtreebuild;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * 分类树表工具
 * @author:xiezhiwei
 * @email:xiezhiwei@niwodai.net
 * @date:2017-09-26
 *
 */
public class TreeUtils {
	
	 /** 
     * 使用递归方法建树 
     * @param treeNodes 
     * @return 
     */  
    public static List<ScmTbtreebuild> buildByScmTbtreebuild(List<ScmTbtreebuild> treeNodes) {  
        List<ScmTbtreebuild> trees = new ArrayList<ScmTbtreebuild>();  
        for (ScmTbtreebuild treeNode : treeNodes) {  
            if ("0".equals(treeNode.getFdParentNodeCode())) {  
                trees.add(findChildren(treeNode,treeNodes));  
            }  
        }  
        return trees;  
    }

    /**
     * 使用递归方法建树
     * @param treeNodes
     * @return
     */
    public static List<ScmTbtreebuild> buildByScmTbtreebuildTwo(List<ScmTbtreebuild> treeNodes) {
        List<ScmTbtreebuild> trees = new ArrayList<ScmTbtreebuild>();
        for (ScmTbtreebuild treeNode : treeNodes) {
            if ("0".equals(treeNode.getFdParentNodeCode())) {
                trees.add(findChildrenTwo(treeNode,treeNodes));
            }
        }
        return trees;
    }
  
    /** 
     * 递归查找子节点 
     * @param treeNodes
     * @return
     */
    public static ScmTbtreebuild findChildren(ScmTbtreebuild treeNode, List<ScmTbtreebuild> treeNodes) {
        for (ScmTbtreebuild it : treeNodes) {
            if(treeNode.getFsNodeCode().equals(it.getFdParentNodeCode())) {  
                if (treeNode.getScmTbtreebuildList() == null) {  
                    treeNode.setScmTbtreebuildList(new ArrayList<ScmTbtreebuild>());  
                }  
                treeNode.getScmTbtreebuildList().add(findChildren(it,treeNodes));  
            }  
        }  
        return treeNode;  
    }

    /**
     * 递归查找子节点 查找至二级 截止
     * @param treeNodes
     * @return
     */
    public static ScmTbtreebuild findChildrenTwo(ScmTbtreebuild treeNode,List<ScmTbtreebuild> treeNodes) {
        for (ScmTbtreebuild it : treeNodes) {
            if(it.getFdNodeLevel()<3){
                if(treeNode.getFsNodeCode().equals(it.getFdParentNodeCode())) {
                    if (treeNode.getScmTbtreebuildList() == null) {
                        treeNode.setScmTbtreebuildList(new ArrayList<ScmTbtreebuild>());
                    }
                    treeNode.getScmTbtreebuildList().add(findChildrenTwo(it,treeNodes));
                }
            }
        }
        return treeNode;
    }



}
